var  fs=  require('fs');
module.exports={
    readfile:function (path, recall){//异步执行
        fs.readFile(path, function(err, data){
            if(err){
                console.log(err);
                recall('文件不存在');
            }else{
                recall(data.toString());
                // res.write(data);  //error : write after end
                console.log(data.toString());
            }
        });
        console.log("异步方法执行完毕");
    },
    readFileSync:function (path){
        var data = fs.readFileSync(path, 'utf-8');
        console.log("同步方法执行完毕");
        console.log(data.toString());
        return data;
    },
    writefile:function(path,data,recall){    //异步方式
        fs.writeFile(path,  data,  function  (err)  {
            if  (err)  {
                throw  err;
            }
            console.log('It\'s  saved!');  //文件被保存
            recall('写文件成功');
          });
    },
    writeFileSync:function(path,data){  //同步方式
        fs.writeFileSync(path,  data);
        console.log("同步写文件完成");
    },
    readImg:function(path,res){
        fs.readFile(path,function(err,  file)  {
            if  (err)  {
                console.log(err);
                return;
            }else{
                console.log("输出文件");
                //res.writeHead(200,  {'Content-Type':'image/jpeg'});
                res.write(file);
                res.end();
            }
        });
    }
};